package com.yong.android.framework.utils;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

/**
 * Description:
 * Author:yong
 * Date:2017-12-14
 */

public class CompatCountDownTimer {
    private OnTimeCountListener mOnTimeCountListener;
    /**
     * Millis since epoch when alarm should stop.
     */
    private long mMillisInFuture;

    /**
     * The interval in millis that the user receives callbacks
     */
    private final long mCountdownInterval;

    private long mStopTimeInFuture;

    /**
     * boolean representing if the timer was cancelled
     */
    private boolean mCancelled = false;
    private boolean mStarted = false;

    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link OnTimeCountListener#onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link OnTimeCountListener#onTick(long)} callbacks.
     */
    public CompatCountDownTimer(long millisInFuture, long countDownInterval) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }

    /**
     * Cancel the countdown.
     */
    public synchronized final void cancel() {
        mCancelled = true;
        mStarted = false;
        mHandler.removeMessages(MSG);
    }

    public boolean isCancelled() {
        return mCancelled;
    }

    public boolean isStarted() {
        return mStarted;
    }

    /**
     * Start the countdown.
     */
    public synchronized final CompatCountDownTimer start() {
        mCancelled = false;
        mStarted = true;
        if (mMillisInFuture <= 0) {
            mOnTimeCountListener.onFinish();
            mStarted = false;
            return this;
        }
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;
    }

    public void setOnTimeCountListener(OnTimeCountListener onTimeCountListener) {
        this.mOnTimeCountListener = onTimeCountListener;
    }

    private static final int MSG = 1;

    public void setMillisInFuture(long millisInFuture) {
        this.mMillisInFuture = millisInFuture;
    }

    // handles counting down
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            synchronized (CompatCountDownTimer.this) {
                if (mCancelled) {
                    return;
                }

                final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();

                if (millisLeft <= 0) {
                    mStarted = false;
                    mOnTimeCountListener.onFinish();
                } else if (millisLeft < mCountdownInterval) {
                    // no tick, just delay until done
                    sendMessageDelayed(obtainMessage(MSG), millisLeft);
                } else {
                    long lastTickStart = SystemClock.elapsedRealtime();
                    mOnTimeCountListener.onTick(millisLeft);

                    // take into account user's onTick taking time to execute
                    long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();

                    // special case: user's onTick took more than interval to
                    // complete, skip to next interval
                    while (delay < 0) delay += mCountdownInterval;

                    sendMessageDelayed(obtainMessage(MSG), delay);
                }
            }
        }
    };

    public interface OnTimeCountListener {

        void onFinish();

        void onTick(long millisUntilFinished);
    }




}
